<template>
  <div class="update-birthday-container">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onConfirm"
      @cancel="$emit('closeBirthdayProup')"
    />
  </div>
</template>

<script>
import { updateUserProfileAPI } from '../../../api/index.js'
import dayjs from 'dayjs'
export default {
  name: 'updateBirthday',
  model: {
    prop: 'birthday',
    event: 'update-birthday'
  },
  props: {
    birthday: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      // new Date中设置的月份是0-11, 对应1-12月,在真实表现时,需要对当前月份+-1
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.birthday)
    }
  },
  methods: {
    // 确认
    async onConfirm (value) {
      this.$toast.loading({
        duration: 0, // 持续展示加载状态弹框的毫秒数,当设置为0时,表示持续加载,需要主动关闭
        message: '加载中...',
        forbidClick: true // 是否禁用背景点击
      })
      value = dayjs(value).format('YYYY-MM-DD')
      try {
        await updateUserProfileAPI({
          birthday: value
        })
        this.$emit('update-birthday', value)
        // 关闭弹出层
        this.$emit('closeBirthdayProup')
        this.$toast.success('修改生日成功')
      } catch (error) {
        console.log(error)
        this.$toast.fail('修改生日失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
